package cn.edu.gsau.info.service.impl;


import cn.edu.gsau.info.dao.ZhidaojingsaiDao;
import cn.edu.gsau.info.exception.BusinessException;
import cn.edu.gsau.info.models.*;
import cn.edu.gsau.info.myenum.ModuleIdClass;
import cn.edu.gsau.info.service.IZhengmingcailiaoService;
import cn.edu.gsau.info.service.IZhidaojingsaiService;
import cn.edu.gsau.info.vo.JinsaiRules;
import cn.edu.gsau.info.vo.Review;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.xml.internal.ws.handler.HandlerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jinjiang
 * @since 2022-11-17
 */

@Service
public class ZhidaojingsaiServiceImpl extends ServiceImpl<ZhidaojingsaiDao, Zhidaojingsai> implements IZhidaojingsaiService {
    @Autowired
    ZhidaojingsaiDao zhidaojingsaiDao;
    @Autowired
    IZhengmingcailiaoService zhengmingcailiaoService;

    @Autowired ReviewServiceImpl reviewService;

    @Autowired
    ShenheServiceImpl shenHeService ;

    @Autowired
    UserServiceImpl userService;

    /**
     * @description: 更新指导竞赛添加表单和证明材料
     * @author: zwh
     * @date:
     * @param:
     * @return:
     **/
    @Override
    @Transactional(readOnly = false)
    public boolean update(Zhidaojingsai zhidaojingsai) {

        //审核完成登记表用户无法更新
        Zhidaojingsai zhidaojingsai1 = zhidaojingsaiDao.selectById(zhidaojingsai.getId());
        if ("审核已通过".equals(zhidaojingsai.getNode())||"待审核".equals(zhidaojingsai.getNode())) {
            throw new BusinessException(Code.BUSINESS_EXCEPTION, "操作异常");
        }
        //查询审核人和审核人姓名
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();
        reviewQueryWrapper.eq("moduleId", ModuleIdClass.ZHIDAOJINGSAI_id);
        Review review = reviewService.getOne(reviewQueryWrapper);
        User user = userService.getById(review.getUserId());
        zhidaojingsai.setManager(user.getNickName());
        zhidaojingsai.setStatus("进行中");
        zhidaojingsai.setNode("待审核");
        zhidaojingsai.setUpdateTime(new Date());
        //更新指导竞赛
        zhidaojingsaiDao.updateById(zhidaojingsai);


        //更新证明材料
        if (zhidaojingsai.getFileList() != null && zhidaojingsai.getFileList().size() > 0) {
            //删除证明材料
            System.out.println("证明材料");
            QueryWrapper<Zhengmingcailiao> zhengmingcailiaoQueryWrapper = new QueryWrapper<>();
            zhengmingcailiaoQueryWrapper.eq("recoreId", zhidaojingsai.getId());
            zhengmingcailiaoService.remove(zhengmingcailiaoQueryWrapper);


            //添加证明材料
            for (Zhengmingcailiao zhengmingcailiao : zhidaojingsai.getFileList()) {
                zhengmingcailiao.setRecoreId(zhidaojingsai.getId());
                zhengmingcailiaoService.save(zhengmingcailiao);

            }
        }

        return true;
    }


    /**
     * @description: 添加证明材料和指导竞赛 分配审核人 TODO
     * @author: zwh
     * @date: 11 24
     * @param:
     * @return:
     **/
    @Override
    @Transactional(readOnly = false)
    public boolean save(Zhidaojingsai zhidaojingsai) {
        //查询审核人和审核人姓名
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();
        reviewQueryWrapper.eq("moduleId", ModuleIdClass.ZHIDAOJINGSAI_id);
        Review review = reviewService.getOne(reviewQueryWrapper);
        User user = userService.getById(review.getUserId());


        //初始化登记表状态
        zhidaojingsai.setNode("待审核");
        zhidaojingsai.setStatus("进行中");
        zhidaojingsai.setCreateTime(new Date());
        zhidaojingsai.setUpdateTime(new Date());
        zhidaojingsai.setDeleted(0);
        zhidaojingsai.setManager(user.getUserName());
        zhidaojingsai.setCreateBy(zhidaojingsai.getTianbaoName());

        //添加登记表和证明材料
        int insert = zhidaojingsaiDao.insert(zhidaojingsai);
        for (Zhengmingcailiao zhengmingcailiao : zhidaojingsai.getFileList()) {
            zhengmingcailiao.setRecoreId(zhidaojingsai.getId());
            zhengmingcailiaoService.save(zhengmingcailiao);

        }


        return true;
    }


    /**
     * @description: 通过rules查询
     * @author: zwh
     * @date: 11 26
     * @param:
     * @return:
     **/
    @Override
    public IPage<Zhidaojingsai> selectRules(JinsaiRules rules) {

        Page<Zhidaojingsai> zhidaojingsaiPage = new Page<>(rules.getPageIndex(), rules.getPageSize());
        QueryWrapper<Zhidaojingsai> zhidaojingsaiQueryWrapper = new QueryWrapper<>();
        if (rules.getTianbaoId() != null) {
            zhidaojingsaiQueryWrapper.eq("tianbaoId", rules.getTianbaoId());
        }
        if (rules.getNode() != null && rules.getNode().length() != 0) {
            zhidaojingsaiQueryWrapper.eq("node", rules.getNode());
        }
        if (rules.getJingsaiName() != null && rules.getJingsaiName().length() != 0) {
            zhidaojingsaiQueryWrapper.like("jingsaiName", rules.getJingsaiName());
        }
        Page<Zhidaojingsai> zhidaojingsaiPage1 = zhidaojingsaiDao.selectPage(zhidaojingsaiPage, zhidaojingsaiQueryWrapper);
        return zhidaojingsaiPage1;

        //查询审核流程


    }


    /**
     * @description: 删除证明材料和指导竞赛等级表
     * @author: zwh
     * @date: 11 26
     * @param:
     * @return:
     **/
    @Override
    @Transactional(readOnly = false)
    public boolean delete(Long id) {

        //审核完成登记表用户无法删除
        Zhidaojingsai zhidaojingsai = zhidaojingsaiDao.selectById(id);
        if ("审核已通过".equals(zhidaojingsai.getNode())||"待审核".equals(zhidaojingsai.getNode())) {
            throw new BusinessException(Code.BUSINESS_EXCEPTION, "操作异常");
        }
        zhidaojingsaiDao.deleteById(id);
        QueryWrapper<Zhengmingcailiao> zhengmingcailiaoQueryWrapper = new QueryWrapper<>();
        zhengmingcailiaoQueryWrapper.eq("recoreId", id);
        zhengmingcailiaoService.remove(zhengmingcailiaoQueryWrapper);
        return true;
    }


    /**
     * @description: 通过id 查询
     * @author: zwh
     * @date: 11 26
     * @param:
     * @return:
     **/
    public Zhidaojingsai getById(Long id) {
        Zhidaojingsai zhidaojingsai = zhidaojingsaiDao.selectById(id);

        //证明材料查询
        QueryWrapper<Zhengmingcailiao> queryWrapper = new QueryWrapper();
        queryWrapper.eq("recoreId", id);
        queryWrapper.eq("moduleId", 7);
        List<Zhengmingcailiao> zhengmingcailiaos = zhengmingcailiaoService.list(queryWrapper);
        System.out.println(zhengmingcailiaos);
        zhidaojingsai.setFileList((ArrayList<Zhengmingcailiao>) zhengmingcailiaos);


        //查询审核流程
        QueryWrapper<Shenhe> shenheQueryWrapper = new QueryWrapper<>();
        shenheQueryWrapper.eq("recoreId",id);

        List<Shenhe> shenhes = shenHeService.list(shenheQueryWrapper);
        System.out.println(shenhes);
        zhidaojingsai.setShenhes(shenhes);
        return zhidaojingsai;
    }

    /**
     * @description: 指导竞赛审核业务 TODO
     * @author: zwh
     * @date: 11 28
     * @param:
     * @return:
     **/
    public boolean check(Zhidaojingsai zhidaojingsai) {
        //查询审核id和上次的审核状态
        QueryWrapper<Shenhe> shenheQueryWrappenew=new QueryWrapper<>();
        QueryWrapper<Review> reviewQueryWrapper = new QueryWrapper<>();

        shenheQueryWrappenew.eq("recoreId",zhidaojingsai.getId());


        //查询最后一次的审核结果,若通过不允许再次通过
        List<Shenhe> shenhes = shenHeService.list(shenheQueryWrappenew);

        if (shenhes.size()!=0&&shenhes.get(shenhes.size()-1).getShenHeResult().equals("审核已通过"))
        {
            if(zhidaojingsai.getShenhe().getShenHeResult().equals("审核已通过"))
            throw new  HandlerException("失败","操作异常");
        }

        //查询审核人信息
        // TODO: 2022/11/30  
        User user = userService.getById(7);


        //判断是否通过
        if (zhidaojingsai.getShenhe().getShenHeResult().equals("审核已通过")) {
            //更新指导竞赛表
            zhidaojingsai.setStatus("流转完成");
            zhidaojingsai.setNode("审核已通过");
            zhidaojingsai.setManager(user.getNickName());
            zhidaojingsaiDao.updateById(zhidaojingsai);

            //添加审核记录

            Shenhe shenhe = zhidaojingsai.getShenhe();
            shenhe.setShenHeTime(new Date());
            shenhe.setModuleId(ModuleIdClass.ZHIDAOJINGSAI_id);
            shenhe.setRecoreId(zhidaojingsai.getId());
            shenhe.setShenheId(user.getId());
            shenhe.setShenHeName(user.getNickName());
            shenhe.setShenHeTime(new Date());

            shenhe.setTijiaoTime(zhidaojingsai.getUpdateTime());
            shenhe.setShenHeResult("审核已通过");
            shenhe.setTijiaoId(zhidaojingsai.getTianbaoId());
            shenhe.setTijiaoMemo(zhidaojingsai.getMemo());
            shenhe.setTijiaoName(zhidaojingsai.getTianbaoName());
            boolean save = shenHeService.save(shenhe);
            return save;
        } else {
            System.out.println(zhidaojingsai.getShenhe());
            //更新指导竞赛表
            zhidaojingsai.setStatus("进行中");
            zhidaojingsai.setNode("审核未通过");
            zhidaojingsai.setManager(user.getNickName());
            zhidaojingsaiDao.updateById(zhidaojingsai);

            //添加审核记录
            Shenhe shenhe =zhidaojingsai.getShenhe();
            shenhe.setModuleId(ModuleIdClass.ZHIDAOJINGSAI_id);
            shenhe.setRecoreId(zhidaojingsai.getId());
            shenhe.setShenheId(user.getId());
            shenhe.setShenHeName(user.getNickName());
            shenhe.setShenHeResult("审核未通过");
            shenhe.setTijiaoId(zhidaojingsai.getTianbaoId());
            shenhe.setShenHeTime(new Date());


            shenhe.setTijiaoTime(zhidaojingsai.getUpdateTime());

            shenhe.setTijiaoMemo(zhidaojingsai.getMemo());
            shenhe.setTijiaoName(zhidaojingsai.getTianbaoName());
            boolean save = shenHeService.save(shenhe);
            
            return save;
            //添加审核记录
        }

        
    }


}
